package com.qf.keleechart.controller;

import com.qf.keleechart.entity.Orde;
import com.qf.keleechart.entity.Product;
import com.qf.keleechart.service.OrdeService;
import com.qf.keleechart.service.ProductService;
import com.qf.keleechart.utils.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 可乐
 * @Description:
 * @CreateTime: 2022-10-21  19:19
 */
@RestController
public class Echart {
	@Autowired
	OrdeService ordeService;

	@Autowired
	ProductService productService;

	@RequestMapping("/bar")
	public Data getdata(){
		Data data=new Data( );
		//查询所有订单中的订单编号
		List< Integer > productIds=new ArrayList<>( );

		List< Orde > list=ordeService.list( );
		list.forEach( s->productIds.add( s.getProductid() ) );

		//查询所有的商品名称
		List< Product > products=productService.listByIds( productIds );
		List< String > names=new ArrayList<>( );
		products.forEach( s->names.add( s.getName() ) );

		//查询订单中商品的销量
		List< Integer > totals=new ArrayList<>( );
		list.forEach( s->totals.add( s.getTotal() ) );

		data.setCategories( names );
		data.setValues( totals );

		return data;
	}
}
